const req = require('../../utils/request.js')
let arrs=[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ids:[],
content:[],
img:[
  "../../img/user.png",
  "../../img/gs.png",
  "../../img/bq.png"
]
  },
  swichNav: function (e) {
    let arr=[]
    let cur = e.target.dataset.current;
   
    arr=arr.concat(cur)
    console.log(arr)
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: arr
      })
      console.log(this.data.currentTab)
    }
  },
  checkButtonTap: function (e) {
    let id = Number(e.currentTarget.dataset.cateid);
    arrs = arrs.concat(id);
    let arr=this.data.content;
    for (let i = 0; i < arr.length; i++) {
      for (let k = 0; k < arr[i].children.length;k++){
        if (arr[i].children[k].cateid == id) {
          if (arr[i].children[k].selected == 0) {
            arr[i].children[k].selected = 1;
          } else {
            arr[i].children[k].selected = 0;
          }
        }
      }
   
    }
    this.setData({
      content: arr
    })

  },

  sureIds(){
    let self = this
  let ids='['+arrs+']'
    req.networkpost("v1/category/addUserCategories", { category_ids: ids}).then((res) => {
      if (res.data.result_code == 200) {
        wx.showToast({
          title: '增加成功',
          success(){
            self.setData({
              content:[]
            })
            self.onLoad()
          }
        })
      } else {
        wx.showToast({
          title: res.data.message,
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTab()
  
  },
  getTab(){
    let that=this
    wx.showLoading({
      title: '加载中',
    })
    req.networkget("v1/category/getAllCategories").then(res => {
      if (res.data.result_code == 200) {
        console.log(res,'biaoqian')
        that.setData({
          content: res.data.result
        })
        wx.hideLoading()
        this.data.content[0].children[0].checked = true;
      } else {
        wx.showToast({
          title: res.data.message,
        })
        wx.hideLoading()
      }
      console.log(res, "列表")
    }).catch(res => {
      wx.showToast({
        title: "请求失败"
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})